package com.eascs.web.o2o.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.eascs.common.util.StringUtil;
import com.eascs.site.tag.select.SelectDataService;
import com.eascs.web.o2o.web.constant.CacheNameConstant;
import com.eascs.web.o2o.web.dao.WebAdminDao;

@Service
public class SelectDataServiceImpl  implements SelectDataService {

	@Autowired
	WebAdminDao webAdminDao;
	
	@Cacheable(CacheNameConstant.SELECT_DATA)
	public List<Object[]> getDataSouce(String dataSouceType, String dataSouceFrom) {
		
		List<Object[]> dataSouceList = new ArrayList<>();
		StringBuffer sql = new StringBuffer(
				"select b.item_code,b.item_name from pub_datadictionary a left join pub_dditem b on a.dd_no = b.dd_no  where a.is_lock = 'N' and b.is_lock = 'N'  ");
		if (StringUtil.isNotBlank(dataSouceType)) {
			if (dataSouceType.equals("OTO_LOAN_TYPE_SB")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('BK','FQ','EA') ");
				sql.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsList(sql.toString(), "OTO_LOAN_TYPE");
			}else if (dataSouceType.equals("O2O_LOAN_TYPE_BP")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('BP','BP_JX','SG','YC','SC') "); // BK BP,只包含终端贷、品牌贷-厂家、品牌贷-经销商
				sql.append(" order by b.item_order asc ");
				dataSouceList = webAdminDao.findObjectsList(sql.toString(), "OTO_LOAN_TYPE");
			} 
			else if (dataSouceType.equals("OTOREPAYMENTWAY_SB")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('AKT','RMT') "); // AKT RMT
				sql.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsList(sql.toString(), "OTOREPAYMENTWAY");
			} else if (dataSouceType.equals("BANKTYPEO2O_SB")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('GACZ','BC','EA') ");
				sql.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsList(sql.toString(), "BANKTYPEO2O");
			} else if (dataSouceType.equals("REGIONALNAME_SB")){
				sql.setLength(0);
				sql.append("select DISTINCT(regional_name),regional_name from o2o_platform_regional");
				dataSouceList = webAdminDao.findObjectsList(sql.toString());
			} else if (dataSouceType.equals("province")){//自然省
				sql.append(" and a.dd_code = ?");
				sql.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsListForCrm(sql.toString(), dataSouceType);
			} else if (dataSouceType.equals("city")){//地级市
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.parent_code = ?");
				sql.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsListForCrm(sql.toString(), dataSouceType,dataSouceFrom);
			}else {
				sql.append(" and a.dd_code = ?");
				sql.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsList(sql.toString(), dataSouceType);
			}
		}
		// 查询新系统增量部分数据字典
		if (dataSouceList.isEmpty()) {
			StringBuffer sb = new StringBuffer(
					"select b.item_code,b.item_name from pub_datadictionary_o2o a left join pub_dditem_o2o b on a.dd_no = b.dd_no  where a.is_lock = 'N' and b.is_lock = 'N'  ");
			if (dataSouceType.equals("SUBJECT_SB")) {
				sb.append(" and a.dd_code = ?");
				sb.append(" and b.item_code in ('BZJ','GLF','FXBCJ') ");
				sb.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsList(sb.toString(), "SUBJECT");
			} else {
				sb.append(" and a.dd_code = ?");
				sb.append(" order by b.item_order desc ");
				dataSouceList = webAdminDao.findObjectsList(sb.toString(), dataSouceType);
			}
		}
		return dataSouceList;
	}

}
